// pages/my/address/index.js
const app = getApp(); // 获取应用实例

Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: 0, alist: [], ispop: 0,
    accepter: '', phone: '', dizhi: '', id: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.userInfo();
    this.getList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 用户信息
  userInfo: function (e) {
    var uinfo = wx.getStorageSync('uinfo');
    if (!uinfo) {
      wx.showToast({
        title: '没有登录', icon: 'none',
      }); return
    }
    this.setData({
      uid: uinfo.uid,
    });
  },

  // 数据列表
  getList: function (e) {
    var formData = {
      uid: this.data.uid,
    };
    var _this = this;
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'my/address/list',
      data: formData,
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        _this.setData({ 
          alist: res.data,
        });
      },
      error: function (err) {
        console.log(err);
      }
    });
  },

  // 表单弹窗
  popAdd: function (e) {
    this.setData({
      ispop: 1,
    });
  },

  // 表单弹窗
  popClose: function (e) {
    this.setData({
      ispop: 0,
    });
  },

  // 收件人
  toAccepter: function (e) {
    console.log(e);
    this.setData({
      accepter: e.detail.value,
    });
  },

  // 手机号
  toPhone: function (e) {
    console.log(e);
    this.setData({
      phone: e.detail.value,
    });
  },

  // 收件人
  toDizhi: function (e) {
    console.log(e);
    this.setData({
      dizhi: e.detail.value,
    });
  },

  // 去修改
  popEdit: function (e) {
    console.log(e);
    var id = e.target.dataset.id;
    var accepter = e.target.dataset.accepter;
    var phone = e.target.dataset.phone;
    var dizhi = e.target.dataset.dizhi;
    this.setData({
      id: id, accepter: accepter,
      phone: phone, dizhi: dizhi,
      ispop: 1,
    });
  },
  
  // 保存
  toSave: function (e) {
    var formData = {
      id: this.data.id,
      uid: this.data.uid,
      accepter: this.data.accepter,
      phone: this.data.phone,
      dizhi: this.data.dizhi,
    };
    console.log(formData); //return
    var _this = this;
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'my/address/modify',
      data: formData,
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        _this.setData({ ispop: 0 });
        _this.getList();
      },
      error: function (err) {
        console.log(err);
      }
    });
  },
  
  // 设置默认
  toDefault: function (e) {
    var formData = {
      id: e.target.dataset.id,
    };
    console.log(formData); //return
    var _this = this;
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'my/address/todefault',
      data: formData,
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        _this.getList();
      },
      error: function (err) {
        console.log(err);
      }
    });
  },
  
  // 删除
  toDel: function (e) {
    var formData = {
      id: e.target.dataset.id,
    };
    console.log(formData); //return
    var _this = this;
    wx.request({
      method: 'post',
      url: app.globalData.xcxApi + 'my/address/del',
      data: formData,
      header: { 'Content-Type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        res = res.data;
        if (res.code < 0) {
          return
        }
        _this.getList();
      },
      error: function (err) {
        console.log(err);
      }
    });
  },
})